package cn.xhjava.crm.workbench.service.impl;

import cn.xhjava.crm.workbench.domain.Activity;
import cn.xhjava.crm.workbench.domain.DateDataMapVO;
import cn.xhjava.crm.workbench.domain.FunnelVO;
import cn.xhjava.crm.workbench.mapper.ActivityMapper;
import cn.xhjava.crm.workbench.service.ActivityService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Author:   XiaHu
 * Date:     2023/6/27
 * 市场活动
 */
@Service
public class ActivityServiceImpl implements ActivityService {
    private static final Logger log = LoggerFactory.getLogger(ActivityServiceImpl.class);
    @Autowired
    private ActivityMapper activityMapper;

    // 创建市场活动
    @Override
    public int add(Activity activity) throws Exception {
        log.info("add Activity {}", activity.toString());
        return activityMapper.insert(activity);
    }


    @Override
    public Activity selectDetailById(String id) throws Exception {
        return activityMapper.selectDetailByPrimaryKey(id);
    }

    // 分页查询
    @Override
    public List<Activity> selectForPage(Map<String, Object> map) throws Exception {
        return activityMapper.selectForPage(map);
    }

    @Override
    public int selectCountForPage(Map<String, Object> map) throws Exception {
        return activityMapper.selectCountForPage(map);
    }

    @Override
    public int delete(String[] ids) throws Exception {
        int res = 1;
        for (String id : ids) {
            activityMapper.deleteByPrimaryKey(id);
        }

        return res;
    }


    @Override
    public int addList(List<Activity> activityList) throws Exception {
        int res = 0;
        for (Activity activity : activityList) {
            activityMapper.insert(activity);
            res++;
        }

        return res;
    }

    @Override
    public List<Activity> selectAll() throws Exception {

        return activityMapper.selectAll();
    }

    @Override
    public int update(Activity activity) throws Exception {
        return activityMapper.updateByPrimaryKeySelective(activity);
    }

    @Override
    public List<Activity> selectList(String[] ids) throws Exception {
        List<Activity> res = new ArrayList<>();
        for (String id : ids) {
            Activity activity = activityMapper.selectByPrimaryKey(id);
            if (null != activity) {
                res.add(activity);
            }
        }

        if (res.size() > 0) {
            return res;
        }
        return null;
    }

    @Override
    public List<Activity> selectClueActivityRelationByClueId(String id) throws Exception {
        return activityMapper.selectClueActivityRelationByClueId(id);
    }

    @Override
    public List<Activity> selectActivityByNameAndClueId(Map<String, Object> map) throws Exception {
        return activityMapper.selectActivityByNameAndClueId(map);
    }

    @Override
    public List<Activity> selectActivityByNameAndContactsId(Map<String, Object> map) throws Exception {
        return activityMapper.selectActivityByNameAndContactsId(map);
    }

    @Override
    public List<Activity> selectDetailListById(String[] ids) throws Exception {
        List<Activity> res = new ArrayList<>();
        for (String id : ids) {
            Activity activity = activityMapper.selectDetailByPrimaryKey(id);
            if (null != activity) {
                res.add(activity);
            }
        }

        if (res.size() > 0) {
            return res;
        }
        return null;
    }

    @Override
    public List<Activity> selectContactsActivityRelationByContactsId(String id) throws Exception {
        return activityMapper.selectContactsActivityRelationByContactsId(id);
    }

    @Override
    public List<Activity> selectLikeName(String name) throws Exception {
        return activityMapper.selectLikeName(name);
    }

    @Override
    public DateDataMapVO queryActivityCountGroupByCreateTime() throws Exception{
        List<FunnelVO> funnelVOList = activityMapper.queryActivityCountGroupByCreateTime();
        DateDataMapVO res = new DateDataMapVO();
        LinkedList<String> date = new LinkedList();
        LinkedList<String> data = new LinkedList();
        for (FunnelVO vo : funnelVOList) {
            date.addLast(vo.getName());
            data.addLast(String.valueOf(vo.getValue()));
        }
        res.setDate(date);
        res.setData(data);
        return res;
    }
}
